package com.museum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.museum.base.BaseResponse;
import com.museum.controller.APi.feignAPI.FeignAPIController;
import com.museum.entity.DO.DepLog;
import com.museum.entity.DTO.DepLogCol;
import com.museum.mapper.DepLogMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ahm
 * @since 2021-03-13
 */
@Service
public class DepLogServiceImpl extends ServiceImpl<DepLogMapper, DepLog>  {
    @Autowired
    private FeignAPIController feignAPIController;
    @Autowired
    private com.museum.mapper.DepLogMapper depLogMapper;


    /**
     * @param
     * @param request
     * @Desciption 添加日志
     * @return
     */

    public int addDepLog(DepLog depLog, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        BaseResponse<JSONObject> userMessageByUSerId = feignAPIController.getUserMessageByUSerId(token);
        JSONObject data = userMessageByUSerId.getData();

        return  depLogMapper.insert(depLog);

    }

    /**
     * @Description 删除日志
     *
     */
    public int deleteDepLogById(Integer id){
        return  depLogMapper.deleteById(id);
    }

//
//
//
    /**
     * @param
     * @Desciption 条件查询日志
     * @return
     */
    public JSONObject getAllDepLogByPage(long current, long limit, DepLog depLog){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<DepLog> depLogList = new ArrayList<DepLog>();
        Integer id =null;
        Integer areaNumber = null;
        String title = null;
        String logType = null;
        String inputTime = null;
        String recorder = null;
        String recorderName = null;
        String recorderType = null;
        if(depLog!=null){
           id = depLog.getId();
           areaNumber = depLog.getAreaNumber();
           title = depLog.getTitle();
           logType = depLog.getLogType();
           inputTime = depLog.getInputTime();
           recorder = depLog.getRecorder();
           recorderName = depLog.getRecorderName();
           recorderType = depLog.getRecorderType();
        }
        if (id == null || id == 0) {
            id = null;
        }
        if (!StringUtils.isEmpty(id)) {
            map.put("id",id);
        }
        if (!StringUtils.isEmpty(areaNumber)) {
            map.put("areaNumber",areaNumber);
        }
        if (!StringUtils.isEmpty(title)) {
            map.put("title",title);
        }
        if (!StringUtils.isEmpty(logType)) {
            map.put("logType",logType);
        }
        if (!StringUtils.isEmpty(inputTime)) {
            map.put("inputTime",inputTime);
        }

        if (!StringUtils.isEmpty(recorder)) {
            map.put("recorder",recorder);
        }

        if (!StringUtils.isEmpty(recorderName)) {
            map.put("recorderName",recorderName);
        }

        if (!StringUtils.isEmpty(recorderType)) {
            map.put("recorderType",recorderType);
        }

        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        List<DepLog> list = depLogMapper.selectDepLogByPage(map);
        for (DepLog log : list) {
            depLogList.add(log);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",depLogList.size());
        jsonObject.put("DepLogList", depLogList);
        return jsonObject;
    }
}
